package com.ksyun.alert.utils.AggregateOperations;

import com.ksyun.alert.customException.SkyAlertException;
import com.ql.util.express.Operator;

import java.util.List;

/**
 * Avg聚合运算符
 * @author: hantou
 * @date: 2024-05-05
 */
public class AvgOperator extends Operator {
    @Override
    public Object executeInner(Object[] objects) {
        double r = 0d;
        int length = 0;

        if (objects.length > 0 && objects[0] instanceof List){
            List list = (List) objects[0];
            length = list.size();

            if (length > 0) {
                for (int i = 0; i < list.size(); i++) {
                    r = r + (double) list.get(i);
                }
                return r / length;
            } else {
                throw new SkyAlertException("列表不能为空");
            }
        } else {
            throw new SkyAlertException("输入的参数必须是一个列表");
        }
    }
}
